import { setTodoList, getTodoList } from '@/views/todolist/localTodo'

export default {
    namespaced: true,
    state: {
        todoList: getTodoList()
    },
    mutations: {
        SET_TODOLIST(state, data) {
            state.todoList.push(data)
        },
        UPDATE_TODOLIST(state, { todoItem, index }) {
            state.todoList[index] = { ...todoItem }
        },
        DEL_TODOLIST(state, index) {
            state.todoList.splice(index, 1)
        },
        SET_ALLLISTSTATUS(state, status) {
            state.todoList.map(v => {
                v.isCompleted = status
            })
        }
    },
    actions: {
        // 全选
        toggleAllChangeFn({ commit, dispatch }, status) {
            commit("SET_ALLLISTSTATUS", status)
            dispatch("setLocalTodo")
        },
        // 增
        addTodoEnterFn({ commit, dispatch }, data) {
            commit("SET_TODOLIST", data)
            dispatch("setLocalTodo")
        },
        // 改   ||  复选框点击
        enterUpdateTodoFn({ commit, dispatch }, data) {
            commit("UPDATE_TODOLIST", data)
            dispatch("setLocalTodo")
        },
        // 删
        delTodoFn({ commit, dispatch }, index) {
            commit("DEL_TODOLIST", index)
            dispatch("setLocalTodo")
        },
        // 保存到本地
        setLocalTodo({ state }) {
            setTodoList(state.todoList);
        }
    }
}